//
//  AppDelegate.m
//
//  Created by : HappyMilk 02
//  Project    : fashionpaint
//  Date       : 2017/11/2
//
//  Copyright (c) 2017年 nike.
//  All rights reserved.
//
// -----------------------------------------------------------------

#import "AppDelegate.h"
//#import "HelloWorldScene.h"

// -----------------------------------------------------------------------

@implementation AppDelegate

// -----------------------------------------------------------------------
// This is where your app starts. It takes two steps
// 1) Setting up Cocos2D, which is done with setupCocos2dWithOptions
// 2) Call your first scene, which is done by overriding startScene

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    [AVOSCloud setApplicationId:@"s2j876jLHPoqtSmojtCIFq8w-gzGzoHsz" clientKey:@"djNidLj24pKwsIOSk3kq9nU0"];
    
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    
    [application registerForRemoteNotifications];
    
    [self configLocalHttpServer];
    

    return YES;
    
}

#pragma mark -- 本地服务器 --
#pragma mark -服务器
#pragma mark - 搭建本地服务器 并且启动
- (void)configLocalHttpServer{
    _localHttpServer = [[HTTPServer alloc] init];
    [_localHttpServer setType:@"_http.tcp"];
    
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    
    NSString *webPath = [[NSBundle mainBundle] pathForResource:@"mazeball" ofType:nil];
    NSLog(@"webPath ----  %@",webPath);
    
    
    if (![fileManager fileExistsAtPath:webPath]){
        
        NSLog(@"File path error!");
    }else{
        NSString *webLocalPath = webPath;
        [_localHttpServer setDocumentRoot:webLocalPath];
        NSLog(@"webLocalPath:%@",webLocalPath);
        [self startServer];
    }
}
- (void)startServer
{
    
    NSError *error;
    if([_localHttpServer start:&error]){
        NSLog(@"Started HTTP Server on port %hu", [_localHttpServer listeningPort]);
        self.port = [NSString stringWithFormat:@"%d",[_localHttpServer listeningPort]];
        //NSString *port = [NSString stringWithFormat:@"http://%@:%d",[self deviceIPAdress],[_localHttpServer listeningPort]];
        //NSLog(@"webpath ---- %@/index.html",port);
    }
    else{
        NSLog(@"Error starting HTTP Server: %@", error);
    }
}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Receive DeviceToken: %@", deviceToken);
    AVInstallation *currentInstallation = [AVInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"注册失败，无法获取设备 ID, 具体错误: %@", error);
}




- (void)applicationDidBecomeActive:(UIApplication *)application{
    
    application.applicationIconBadgeNumber = 0;
}

// -----------------------------------------------------------------------
// This method should return the very first scene to be run when your app starts.



// -----------------------------------------------------------------------

@end























